package com.highgain.displacementtrend.controller;

import com.highgain.displacementtrend.entity.Device;
import com.highgain.displacementtrend.entity.OnLineRateInfo;
import com.highgain.displacementtrend.service.AreaService;
import com.highgain.displacementtrend.service.DeviceService;
import com.highgain.displacementtrend.util.Flexigrid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhanglifeng
 * @date 2021/04/27 16:53
 **/
@Controller
@RequestMapping("/statistics")
class StatisticsController {
    @Resource
    private DeviceService deviceService;

    @Resource
    private AreaService areaService;

    @GetMapping("/onlineRate/list/page")
    public String queryOnlineRatePage(){
        return "statistics/onlineRate/list";
    }


    @PostMapping("/onlineRate/query")
    @ResponseBody
    public Flexigrid queryOnlineRate(Device device, Flexigrid flex, HttpServletRequest request){
        List<OnLineRateInfo> onLineRateInfoList = new ArrayList<>(1);
        OnLineRateInfo onLineRateInfo = deviceService.queryOnlineRate(device);
        Integer areaCode = device.getAreaCode();
        Integer cityCode = device.getCityCode();
        Integer provinceCode = device.getProvinceCode();
        Integer hiddenPointId = device.getHiddenPointId();
        if(areaCode != null) {
            onLineRateInfo.setAreaName(areaService.getAreaName(areaCode).getName());
        }

        if(cityCode != null) {
            onLineRateInfo.setCityName(areaService.getAreaName(cityCode).getName());
        }

        if(provinceCode != null){
            onLineRateInfo.setProvinceName(areaService.getAreaName(provinceCode).getName());
        }

        if(hiddenPointId != null) {
            onLineRateInfo.setHiddenPointName(deviceService.queryHiddenPointName(hiddenPointId));
        }

        onLineRateInfoList.add(onLineRateInfo);
        flex.setTotal(onLineRateInfoList.size());
        flex.setRows(onLineRateInfoList);
        return flex;
    }
}
